﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.Web;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Data;
using System.IO;
using MBSProcessComponent;

namespace MBS
{
    public partial class MovieRevenueReport : System.Web.UI.Page
    {
        ReportDocument objReport = new ReportDocument();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadMovies();
            }
        }

        private void LoadMovies()
        {
            MBSBLC.BLC objBlc=new MBSBLC.BLC();
            MBSCache.addMovieDictionary(objBlc.GetAllMovies());
            ddlMovie.DataSourceID = null;
            ddlMovie.DataSource = MBSCache.getMovieDictionary();
            ddlMovie.DataTextField = "Value";
            ddlMovie.DataValueField = "Key";
            ddlMovie.DataBind();
            //throw new NotImplementedException();
        }

        protected void btnReport_Click(object sender, EventArgs e)
        {
            DataSet dsReport = new DataSet();
            //DateTime StartDate = startDateCalendar.SelectedDate;
            //DateTime EndDate = endDateCalendar.SelectedDate;
            int movie = Convert.ToInt32(ddlMovie.SelectedValue);
            MBSBLC.BLC blc=new MBSBLC.BLC();
            dsReport = blc.GetMovieReport(movie,StartDateTextBox.Text, EndDateTextBox.Text);
            if (dsReport != null)
            {
                //dsReport.WriteXmlSchema(Server.MapPath("~/MovieRevenue.xsd"));
                objReport.Load(Server.MapPath("~/CrystalReports/crpMovieRevenue.rpt"));
                objReport.SetDataSource(dsReport.Tables[0]);
                System.IO.MemoryStream s = (MemoryStream)objReport.ExportToStream(ExportFormatType.PortableDocFormat);
                // the code below will create pdfs in memory and stream them to the browser
                // instead of creating files on disk.
                var _with1 = HttpContext.Current.Response;
                _with1.ClearContent();
                _with1.ClearHeaders();
                _with1.ContentType = "application/pdf";
                _with1.AddHeader("Content-Disposition", "inline; filename=Report.pdf");
                _with1.BinaryWrite(s.ToArray());
                _with1.End();
                // rptDoc.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                //rptDoc.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, HttpContext.Current.Response, true, "JobOrderList Report");
                crpViewerMovie.ReportSource = objReport;
            }
            else
            {
                string noReport = "Report for the selected dates is not available";
                ClientScript.RegisterStartupScript(this.GetType(), "sad", "javascript:showAlert('" + noReport + "')", true);
            }
        }
        protected void startDateCalendar_SelectionChanged(object sender, EventArgs e)
        {
            StartDateTextBox.Text = startDateCalendar.SelectedDate.ToString();
        }

        protected void endDateCalendar_SelectionChanged(object sender, EventArgs e)
        {
            EndDateTextBox.Text = endDateCalendar.SelectedDate.ToString();
        }
    }
}